package com.air.designmodel.three;

/**
 * 布告板,显示最高气温,最低气温,平局气温
 *
 * @author shent
 */
public class StatisticsDisplay implements Observer, DisplayElement {
    private Subject weatherData;
    private float min_templature;
    private float max_templature;
    private float avg_templature;

    private static int count = 0;
    private static float sum_templature = 0;

    public StatisticsDisplay(Subject weatherData) {
        this.weatherData = weatherData;
        weatherData.registerObserver(this);
    }

    @Override
    public void update(float templature, float humidity, float pressure) {
        if (count == 0) {
            min_templature = templature;
            max_templature = templature;
            avg_templature = templature;
            sum_templature += templature;
            count++;
        } else {
            count++;
            sum_templature += templature;
            avg_templature = sum_templature / count;
            min_templature = Math.min(min_templature, templature);
            max_templature = Math.max(max_templature, templature);
        }
        display();
    }

    @Override
    public void notObserver() {
        weatherData.removeObserver(this);
    }

    @Override
    public void observer() {
        weatherData.registerObserver(this);
    }

    @Override
    public void display() {
        System.out.println("平均温度:" + avg_templature + ",最高温度:" + max_templature + ",最低温度:" + min_templature);
    }
}
